﻿using ISolutions.xRM.TechDays.ContactCard.Utilities;
using Microsoft.Xrm.Sdk;
using System;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Media.Capture;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

namespace ISolutions.xRM.TechDays.ContactCard
{
    public sealed partial class CreateContact : Page
    {
        private Byte[] image = null;
        private ImageSource defaultSource;

        public CreateContact()
        {
            this.InitializeComponent();
        }

        private async void Create(object sender, RoutedEventArgs e)
        {
            var contact = new Entity("new_candidate");
            contact["new_firstname"] = Firstname.Text;
            contact["new_lastname"] = Lastname.Text;
            contact["new_name"] = string.Format("{0} {1}", Firstname.Text, Lastname.Text);

            if (image != null)
            {
                contact["entityimage"] = image;
            }

            Firstname.IsEnabled = false;
            Lastname.IsEnabled = false;
            imageButton.IsEnabled = false;
            CreateButton.IsEnabled = false;

            await CrmConnectionManager.OrgServiceProxy.CreateAsync(contact);

            Firstname.IsEnabled = true;
            Lastname.IsEnabled = true;
            imageButton.IsEnabled = true;
            CreateButton.IsEnabled = true;

            Firstname.Text = "";
            Lastname.Text = "";
            ContactImage.Source = defaultSource;
        }

        private async void AddImage(object sender, RoutedEventArgs e)
        {
            var camera = new CameraCaptureUI();
            camera.PhotoSettings.CroppedAspectRatio = new Size(16, 9);
            StorageFile photo = await camera.CaptureFileAsync(CameraCaptureUIMode.Photo);

            if (photo != null)
            {
                image = await ReadFile(photo);

                var bitmapImage = new BitmapImage();
                var stream = new InMemoryRandomAccessStream();
                await stream.WriteAsync(image.AsBuffer());
                stream.Seek(0);
                bitmapImage.SetSource(stream);
                defaultSource = ContactImage.Source;
                ContactImage.Source = bitmapImage;
            }
        }

        public async Task<byte[]> ReadFile(StorageFile file)
        {
            byte[] fileBytes = null;
            using (IRandomAccessStreamWithContentType stream = await file.OpenReadAsync())
            {
                fileBytes = new byte[stream.Size];
                using (var reader = new DataReader(stream))
                {
                    await reader.LoadAsync((uint)stream.Size);
                    reader.ReadBytes(fileBytes);
                }
            }
            return fileBytes;
        }
    }
}
